<?php

namespace IDock\Insurance\CHAC\Response;

use IDock\Http\Response;
use IDock\Printer\NonCar\NonCarInquiryPrinter;
use IDock\Printer\NonCar\NonCarInquirySinglePrinter;
use IDock\Printer\ProposalStatusPrinter;

class K1PolicyStatusResponse extends AllCHACResponse
{
    /*** @var ProposalStatusPrinter $printer */
    public $printer = ProposalStatusPrinter::class;

    public $urlDecode = true;

    public function printerInit(): Response
    {
        $header = $this->get('responseHead');

        if ($header['responseCode'] != '0000') {
            $this->setError($header['errorMessage'] ?? '');
            return $this;
        }

        $responseBody = $this->get('responseBody', []);

        $eBCallbackData = $responseBody['eBCallbackData'] ?? [];

        $nvhlPolicyNo = $responseBody['nvhlPolicyNo'] ?? '';

        $biStatus = $ciStatus = '';
        $biMsg = $ciMsg = '';

        foreach ($eBCallbackData as $datum) {
            if ($datum['riskCode'] == '0801') {
                $this->printer->ciPolicyNo = $datum['policyNo'] ?? '';
                $this->printer->ciInquiryNo = $datum['proposalNo'] ?? '';
                $ciStatus = EUnderwritingResponse::statusCode($datum['underWriteInd']);
                $ciMsg = $datum['operateText'] ?? '';
            }

            if ($datum['riskCode'] == '0808') {
                $this->printer->biPolicyNo = $datum['policyNo'] ?? '';
                $this->printer->biInquiryNo = $datum['proposalNo'] ?? '';
                $biStatus = EUnderwritingResponse::statusCode($datum['underWriteInd']);
                $biMsg = $datum['operateText'] ?? '';
            }
        }

        if ($nvhlPolicyNo) {
            $this->printer->nonCarInquiryPrinter = new NonCarInquiryPrinter();
            $nonCarInquirySinglePrinter = new NonCarInquirySinglePrinter;
            $nonCarInquirySinglePrinter->policyNo = $nvhlPolicyNo;
            $this->printer->nonCarInquiryPrinter->noCarInquirySingleAppend($nonCarInquirySinglePrinter);
        }

        $this->printer->underDate = date('Y-m-d H:i:s',$eBCallbackData[0]['operateTime'] / 1000);

        //核保通过
        if ($biStatus.$ciStatus =='U1' or $biStatus.$ciStatus == 'U1U1') {
            $this->printer->statusCode = 'U1';
        }

        $errMsg = array_unique(array_filter([$biMsg,$ciMsg]));

        //下发修改
        if (in_array('U2',[$biStatus,$ciStatus])) {
            $this->printer->messageTip = implode("\r\n",$errMsg);
            $this->printer->statusCode = 'U2';
        }

        //人工
        if (in_array('U3',[$biStatus,$ciStatus])) {
            $this->printer->messageTip = implode("\r\n",$errMsg);
            $this->printer->statusCode = 'U3';
        }

        //失败
        if (in_array('U4',[$biStatus,$ciStatus])) {
            $this->printer->messageTip = implode("\r\n",$errMsg);
            $this->printer->statusCode = 'U4';
        }

        //拒保
        if (in_array('U6',[$biStatus,$ciStatus])) {
            $this->printer->messageTip = implode("\r\n",$errMsg);
            $this->printer->statusCode = 'U6';
        }

        //已承保
        if (in_array('U5',[$biStatus,$ciStatus])) {
            $this->printer->payDate = date('Y-m-d H:i:s',$eBCallbackData[0]['operateTime'] / 1000);
            $this->printer->statusCode = 'U5';
        }

        //待审核
        if (in_array('U7',[$biStatus,$ciStatus])) {
            $this->printer->statusCode = 'U7';
        }

        return $this;
    }
}
